#region Referencias
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Nextel.Utils;
#endregion

public class DALBase
{
    public static string prCadConexion
    {
        get
        {
            ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["SQLServerConnection"];
            return ConnectionStringSettings.ConnectionString;
        }
    }

    public static String getTelefonoXUsuario(String idUsuario, String spName)
    {
        String lsTelefono = string.Empty;
        using (SqlConnection conn = new SqlConnection(prCadConexion))
        {

            using (SqlCommand cmd = new SqlCommand(spName, conn))
            {

                conn.Open();
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@USUARIO", idUsuario));

                using (SqlDataReader loDr = cmd.ExecuteReader())
                {
                    while (loDr.Read())
                    {

                        lsTelefono = loDr.GetValue(loDr.GetOrdinal("telefono")).ToString().Trim();
                    }
                }
            }
        }
        return lsTelefono;

    }

    public static List<ComboBean> getGeneralBean(String estado, String spName)
    {
        List<ComboBean> lista = new List<ComboBean>();
        
        ComboBean bean =  new ComboBean();
       /* bean.codigo = "";
        bean.nombre = "";
        lista.Add(bean);*/


        using (SqlConnection conn = new SqlConnection(prCadConexion))
        {

            using (SqlCommand cmd = new SqlCommand(spName, conn))
            {

                conn.Open();
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@FLAG", estado));

                using (SqlDataReader loDr = cmd.ExecuteReader())
                {
                    while (loDr.Read())
                    {
                        bean = new ComboBean();
                        bean.codigo = loDr.GetValue(loDr.GetOrdinal("codigo")).ToString().Trim();

                        bean.nombre = loDr.GetValue(loDr.GetOrdinal("nombre")).ToString().Trim();

                        bean.flag = loDr.GetValue(loDr.GetOrdinal("flag")).ToString().Trim();

                        lista.Add(bean);
                    }
                }
            }
        }

        return lista;
    }

    public static List<ComboBean> getGeneralBean(String estado, String spName, String usuarioSesion)
    {
        List<ComboBean> lista = new List<ComboBean>();

        ComboBean bean = null;

        using (SqlConnection conn = new SqlConnection(prCadConexion))
        {

            using (SqlCommand cmd = new SqlCommand(spName, conn))
            {

                conn.Open();
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@FLAG", estado));
                cmd.Parameters.Add(new SqlParameter("@CODIGOUSUARIO_SESION", fnVerNuloStr(usuarioSesion)));
                cmd.Parameters.Add(new SqlParameter("@FLAG_SUPERVISOR", fnVerNuloStr(AbstractBean.ACTIVO)));


                using (SqlDataReader loDr = cmd.ExecuteReader())
                {
                    while (loDr.Read())
                    {
                        bean = new ComboBean();
                        bean.codigo = loDr.GetValue(loDr.GetOrdinal("codigo")).ToString().Trim();

                        bean.nombre = loDr.GetValue(loDr.GetOrdinal("nombre")).ToString().Trim();

                        bean.flag = loDr.GetValue(loDr.GetOrdinal("flag")).ToString().Trim();

                        lista.Add(bean);
                    }
                }
            }
        }

        return lista;
    }


    public static List<ComboBean> getPuntoCercanoBean(String codigocliente, String spName)
    {
        List<ComboBean> lista = new List<ComboBean>();

        ComboBean bean = null;

        using (SqlConnection conn = new SqlConnection(prCadConexion))
        {

            using (SqlCommand cmd = new SqlCommand(spName, conn))
            {

                conn.Open();
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CODIGOCLIENTE", codigocliente));

                using (SqlDataReader loDr = cmd.ExecuteReader())
                {
                    while (loDr.Read())
                    {
                        bean = new ComboBean();
                        bean.codigo = loDr.GetValue(loDr.GetOrdinal("codigo")).ToString().Trim();

                        bean.nombre = loDr.GetValue(loDr.GetOrdinal("nombre")).ToString().Trim();

                        bean.flag = loDr.GetValue(loDr.GetOrdinal("flag")).ToString().Trim();

                        lista.Add(bean);
                    }
                }
            }
        }

        return lista;
    }

    
    public static void delete(String nameSP, String paId, String psHabilitar)
    {
        using (SqlConnection conn = new SqlConnection(prCadConexion))
        {
            using (SqlCommand cmd = new SqlCommand(nameSP, conn))//"USPW_USUARIO_D"
            {
                conn.Open();
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@CODIGOS", paId));
                cmd.Parameters.Add(new SqlParameter("@HABILITAR", psHabilitar));

                cmd.ExecuteNonQuery();

            }
        }

    }

    #region MANEJO DE NULOS

    protected static String fnVerNuloStr(String poObjeto)
    {
        return poObjeto == null ? "" : poObjeto;
    }
    protected static int fnVerNuloInt(object poObjeto)
    {
        return poObjeto != null && !"".Equals(poObjeto.ToString().Trim()) ? int.Parse(poObjeto.ToString().Trim()) : 0;
    }

    protected static Decimal fnVerNuloDec(object poObjeto)
    {
        if (poObjeto == DBNull.Value)
        {
            return 0;
        }
        else
        {
            return (Decimal)poObjeto;
        }
    }

    protected static string getStringFechaYYMMDD(string fecha)
    {

        if (fecha != null && !"".Equals(fecha))
        {
            string[] split = fecha.Split('/');
            if (split.Length >= 3)
            {
                if (split[0].Length < 2) { split[0] = "0" + split[0]; }
                if (split[1].Length < 2) { split[1] = "0" + split[1]; }

                switch (ManagerConfiguration.formatofecha)
                {
                    case ManagerConfiguration.FORMATODDMMYY:
                        return (split[2] + split[1] + split[0]);
                    case ManagerConfiguration.FORMATOMMDDYY:
                        return (split[2] + split[0] + split[1]);
                    default: return (split[2] + split[1] + split[0]);
                }
            }
        }

        return "";

    }


    public static char fnChar(String psString)
    {
        char lsResultado = '0';
        if (psString != null)
        {
            if (psString.Length > 0)
                lsResultado = psString.ToCharArray()[0];
        }

        return lsResultado;
    }

    public static int fnInt(String psString)
    {
        int lsResultado = 0;
        if (psString != null)
        {
            if (StringUtils.validarNumeroEntero(psString))
            {
                lsResultado = Convert.ToInt32(psString);
            }
        }

        return lsResultado;
    }

    public static Int64 fnInt64(String psString)
    {
        Int64 lsResultado = 0;
        if (psString != null)
        {
            if (StringUtils.validarNumeroEntero64(psString))
            {
                lsResultado = Convert.ToInt64(psString);
            }
        }

        return lsResultado;
    }

    public static string getStringFechaYYMMDDHHMM(string fecha)
    {
        if (fecha == null) return "";

        fecha = fecha.Trim();

        if (fecha.Length <= 10)
        {
            return getStringFechaYYMMDD(fecha);
        }
        else if (fecha.Length > 10)
        {
            String[] arrElementosFecha = fecha.Split(' ');
            string cadena = "";
            if (arrElementosFecha.Length > 0)
            {
                cadena = getStringFechaYYMMDD(arrElementosFecha[0]);
            }
            if (arrElementosFecha.Length > 1)
            {
                cadena += ' ' + arrElementosFecha[1];
            }
            return cadena.Trim();
        }

        return "";
    }
    #endregion


}
